import React, { useState, useEffect } from 'react'
import { Form, Input, Button, Card, message } from 'antd'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import OperaPrompt from '../../../components/OperaPrompt'
import BraftEditor from 'braft-editor'
import 'braft-editor/dist/index.css'
import request from '@/utils/request'

export default () => {

    const [form] = Form.useForm()
    const onFinish = (values: any) => {

        values.content = values.content.toHTML()

        request(`/member/agreement-save`, {
            method: 'POST',
            data: { ...values },
        }).then((result) => {
            if (result.code !== 200) {
                message.warning(result.message)
                return
            }
            message.success(result.message)
        })
    }
    useEffect(() => {
        request(`/member/agreement-detail`).then((result) => {
            const data = result.data
            form.setFieldsValue({
                title: data.title,
                content: BraftEditor.createEditorState(data.content)
            })
        })
    }, [])
    return (
        <PageHeaderWrapper title={false}>
            <Card>
                <OperaPrompt dataList={[
                    "协议内容展示使用该网站的一些相关规定",
                    "注册会员时需同意该协议才可继续注册",
                ]} />
                <Form
                    labelCol={{ span: 3 }}
                    wrapperCol={{ span: 15 }}
                    onFinish={onFinish}
                    style={{ marginTop: "20px" }}
                    form={form}
                >
                    <Form.Item label="协议" required name="title">
                        <Input />
                    </Form.Item>

                    <Form.Item label="内容" required name="content">
                        <BraftEditor
                            style={{ border: '1px solid #d1d1d1' }}
                        />
                    </Form.Item>
                    <Form.Item wrapperCol={{ offset: 3 }}>
                        <Button type="primary" htmlType="submit">保存</Button>
                        <Button style={{ marginLeft: "5px" }} onClick={() => {
                            form.resetFields()
                        }}>重置</Button>
                    </Form.Item>
                </Form>
            </Card>
        </PageHeaderWrapper>
    )
}